<template>
  <el-form :model="formData" label-width="100px" :rules="rules" ref="formRef">
    <el-form-item label="表单名称" required prop="name">
      <el-input v-model="formData.name" placeholder="请输入表单名称" />
    </el-form-item>
    <!-- DEBUG: 如果你看到这行注释，说明使用了正确的组件 -->
    <!-- 表单OID自动生成，版本号不需要：两项从创建弹窗移除 -->
    <el-form-item label="排序" prop="order">
      <el-input-number 
        v-model="formData.order" 
        :min="0" 
        :max="9999"
        placeholder="自动分配排序号"
        style="width: 100%"
      />
      <div v-if="!props.editData?.id" class="order-tip">
        新增表单时系统将自动分配排序号
      </div>
    </el-form-item>
    <el-form-item>
      <div class="dialog-footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </div>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { ref, onMounted } from 'vue'

const props = defineProps({
  editData: {
    type: Object,
    default: () => ({})
  }
})

const emit = defineEmits(['close'])

const formRef = ref(null)

const rules = {
  name: [
    { required: true, message: '请输入表单名称', trigger: 'blur' }
  ]
}

const formData = ref({
  name: props.editData.name || '',
  order: props.editData.order || 1,
  status: props.editData.status || '草稿',
  type: 'form',
  projectId: props.editData.projectId || null,
  config: {
    fields: props.editData.config?.fields || [],
    lastUpdated: new Date().toISOString()
  }
})

onMounted(() => {})

const handleConfirm = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate((valid) => {
    if (valid) {
      emit('close', formData.value)
    }
  })
}

const handleCancel = () => {
  emit('close')
}

defineExpose({
  handleConfirm
})
</script>

<style scoped>
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

.order-tip {
  font-size: 12px;
  color: #909399;
  margin-top: 4px;
}
</style> 